package ltd.hxya.novel.admin.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import ltd.hxya.novel.admin.entity.UserRole;
import ltd.hxya.novel.admin.mapper.UserRoleMapper;
import ltd.hxya.novel.admin.service.UserRoleService;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class UserRoleServiceImpl extends ServiceImpl<UserRoleMapper,UserRole> implements UserRoleService {


    @Override
    public int revoke(UserRole userRole) {
        QueryWrapper<UserRole> userRoleQueryWrapper = new QueryWrapper<>();
        userRoleQueryWrapper
                .eq("user_id",userRole.getUserId())
                .eq("role_id",userRole.getRoleId());
        return this.baseMapper.delete(userRoleQueryWrapper);
    }

    /**
     * 根据userIdList进行相应的删除操作
     * @param idList
     */
    @Override
    public void deleteByIds(List<Integer> idList) {
        if (idList ==null){
            return;
        }
        QueryWrapper<UserRole> queryWrapper = new QueryWrapper<>();
        for (Integer integer : idList) {
            queryWrapper.eq("user_id",integer);
            this.baseMapper.delete(queryWrapper);
        }
    }

    /**
     *  1、根据userId，查询出user_role集合
     * @param userId
     * @return
     */
    @Override
    public List<UserRole> getRoleList(Integer userId) {
        QueryWrapper<UserRole> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_id",userId);
        List<UserRole> userRoles = baseMapper.selectList(queryWrapper);
        return userRoles;
    }
}
